const express=require("express")
const router=express.Router();
const pool=require("../pool");

router.get("/news",(req,res)=>{
  
  //1:获取参数nid 新闻编号pno 页码 pageSize
  var id=parseInt(req.query.nid);
  var pno = req.query.pno;
  var pageSize = req.query.pageSize;
  //2:设置默认值
  if(!pno){pno=1}
  if(!pageSize){pageSize=5}
  //2:创建正则表达式
  var reg = /^\d{1,}$/;
  //3:如果验证失败 输出错误信息{code:-1}
  if(!reg.test(id)){
    res.send({code:-1,msg:"新闻标号格式有误"});
    return;
  }
  var sql = " SELECT cid,pid,userimg,user,dates,likes,content";
      sql +=" FROM news_comment";
      sql +=" WHERE pid=?";
      sql +=" ORDER BY dates DESC";
      sql +=" LIMIT ?,?"
  //4:offset 行偏移量    
  var offset = (pno-1)*pageSize;
  //5:页大小造型
  pageSize = parseInt(pageSize);
  
  pool.query(sql,[id,offset,pageSize],(err,result)=>{
    if(err) console.log(err);
    //console.log(result);
    res.send({code:1,data:result});
  })
})

router.get("/v",(req,res)=>{
  
  //1:获取参数nid 新闻编号pno 页码 pageSize
  var id=parseInt(req.query.did);
  //console.log(typeof id,id)
  var pno = req.query.pno;
  var pageSize = req.query.pageSize;
  //2:设置默认值
  if(!pno){pno=1}
  if(!pageSize){pageSize=5}
  //2:创建正则表达式
  var reg = /^\d{1,}$/;
  //3:如果验证失败 输出错误信息{code:-1}
  if(!reg.test(id)){
    res.send({code:-1,msg:"新闻标号格式有误"});
    return;
  }
  var sql = " SELECT cid,pid,userimg,user,dates,likes,content";
      sql +=" FROM video_comment";
      sql +=" WHERE pid=?";
      sql +=" ORDER BY dates DESC";
      sql +=" LIMIT ?,?";
  //4:offset 行偏移量    
  var offset = (pno-1)*pageSize;
  //5:页大小造型
  pageSize = parseInt(pageSize);
  
  pool.query(sql,[id,offset,pageSize],(err,result)=>{
    if(err) console.log(err);
    //console.log(result);
    res.send({code:1,data:result});
  })
})
module.exports=router;